package yxk.operation.moka;

import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import yxk.list.odc.homs.util.OnboardUtil;
import yxk.utils.DateUtil;

import java.util.Date;

/**
 * 转正审核通过后回写摩卡状态
 * 绑定单据  nwg5_hdm_regbasebill_ext
 */
public class HdmRegbaseBillOperation extends AbstractOperationServicePlugIn implements Plugin {
    private static final Log LOG = LogFactory.getLog(HdmRegbaseBillOperation.class);
    OnboardUtil onboardUtil=new OnboardUtil();
    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        String operationKey = e.getOperationKey();
        if("wfauditpass".equals(operationKey) || "submiteffect".equals(operationKey) || "submiteffectlist".equals(operationKey)){
            DynamicObject[] entities = e.getDataEntities();
            String token = "";
            LOG.info("beforeExecuteOperationTransaction");
            for (DynamicObject row : entities) {
                String number = row.getString("person.number");
                //获取入职单
                DynamicObject single = BusinessDataServiceHelper.loadSingle("hom_onbrdinfo", new QFilter("employeeno", QCP.equals, number).toArray());
                LOG.info("beforeExecuteOperationTransaction.single:{}",single);
                if(single != null){
                    String offernumber = single.getString("offernumber");
                    LOG.info("beforeExecuteOperationTransaction.offernumber:{}",offernumber);
                    if(offernumber!=null && !"".equals(offernumber)){
                        //获取候选人编码
                        String candidatenumber = single.getString("candidatenumber");
                        LOG.info("beforeExecuteOperationTransaction.candidatenumber:{}",candidatenumber);
                        if("".equals(token) || token== null){
                            token = onboardUtil.getZhaoPinToken();
                        }

                        if(!"".equals(token) && token != null) {
                            JSONObject param = new JSONObject();
                            param.put("status", "1");
                            Date effectdate = row.getDate("effectdate");
                            String iso8601TimestampFromDateStr = DateUtil.getISO8601TimestampFromDateStr(effectdate);
                            param.put("correctedAt", iso8601TimestampFromDateStr);
                            onboardUtil.getCorrected(token, param.toString(), candidatenumber);
                        }
                    }
                }
            }
        }
    }
}